<template>
  <div class="re-html">
    <!-- <div class="re-img"></div> -->
    <div class="re-body">
      <div class="re-box" v-if="!canregist">
        <div class="re-header">
          <span>欢迎注册</span>
        </div>
        <div class="rebox">
          <Smscode sign="sign"></Smscode>
        </div>
      </div>
      <div v-if="canregist">
        <div
          class="rebox"
          style="
            padding: 20px;
            border-radius: 25px;
            background-color: #d2e2ffa6;
          "
        >
          <el-form
            :model="registerForm"
            status-icon
            :rules="registerRules"
            ref="registerForm"
            label-width="82px"
          >
            <el-form-item label="昵称" prop="username">
              <el-input
                type="text"
                v-model="registerForm.username"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                type="password"
                v-model="registerForm.password"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
              <el-input
                type="password"
                v-model="registerForm.checkPass"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="registerForm.email"></el-input>
            </el-form-item>

            <el-form-item label="性别" prop="sex">
              <el-radio v-model="registerForm.sex" label="1">男</el-radio>
              <el-radio v-model="registerForm.sex" label="2">女</el-radio>
              <el-radio v-model="registerForm.sex" label="3">保密</el-radio>
            </el-form-item>
            <el-form-item label="年龄" prop="age">
              <el-input v-model.number="registerForm.age"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="register()">注册</el-button>
              <el-button @click="login()">登录</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { validateAge, validateEaiReg } from "../plugins/validator.js";
import { encrypt } from "../plugins/rsaEncrypt"; //引入rsaEncrypt.js
import Smscode from "@/components/Smscode";
export default {
  components: { Smscode },
  computed: {
    registercon() {
      return this.$store.getters.getRegisterContion;
    },
  },
  watch: {
    registercon: {
      handler(val) {
        if (val != " ") {
          this.canregist = true;
          this.$store.commit("setSmsContion", {
            key: "register",
            value: " ",
          });
          this.registerForm.phone = val;
        }
      },
    },
  },
  data() {
    var validatePhone = (rule, value, callback) => {
      if (
        value === "" ||
        value === undefined ||
        value === "null" ||
        value === null
      ) {
        callback(new Error("请输入电话号码"));
      } else {
        // eslint-disable-next-line eqeqeq
        if (!/^[1][3-9][0-9]{9}$/.test(value) && value !== "") {
          callback(new Error("请输入正确的电话号码"));
        } else {
          setTimeout(() => {
            this.axios
              .post("/user/IsPhone", {
                phone: this.SmsForm.phone,
              })
              .then((res) => {
                // console.log(res);
                if (res.data.data.code == 200) {
                  this.nophone = true;
                  callback();
                } else {
                  this.nophone = false;
                  callback(new Error("电话号码已被注册"));
                }
              });
          }, 100);
        }
      }
    };
    var checkUsername = (rule, value, callback) => {
      let reg = /^[\u4e00-\u9fa5_a-zA-Z0-9.·-]+$/;
      if (!value) {
        callback(new Error("请输入用户名"));
      } else if (value.length < 1 || value.length > 7) {
        callback(new Error("长度在 1 到 6 个字符"));
      } else if (!reg.test(value)) {
        callback(new Error("姓名不支持特殊字符"));
      } else {
        setTimeout(() => {
          this.axios
            .post("/user/Isun", {
              username: this.registerForm.username,
            })
            .then((res) => {
              if (res.data.data.code == 201) {
                callback();
              } else {
                callback(new Error("昵称已存在"));
              }
            });
        }, 100);
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入密码"));
      } else if (
        !/^(?![\d]+$)(?![a-zA-Z]+$)(?![^\da-zA-Z]+$)([^\u4e00-\u9fa5\s]){6,18}$/.test(
          value
        )
      ) {
        callback(new Error("请输入6-18位数字和字符或者符号的组合"));
      } else {
        if (this.registerForm.checkPass !== "") {
          this.$refs.registerForm.validateField("checkPass");
        }
        callback();
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.registerForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback();
      }
    };
    return {
      nophone: true,
      canregist: false,

      registerForm: {
        username: "",
        password: "",
        checkPass: "",
        email: "",
        phone: "",
        sex: "",
        age: "",
        smscode: "",
      },
      registerRules: {
        //校验用户名
        username: [
          { required: true, validator: checkUsername, trigger: "blur" },
        ],
        password: [
          { required: true, validator: validatePass, trigger: "blur" },
        ],
        checkPass: [
          { required: true, validator: validatePass2, trigger: "blur" },
        ],
        email: [{ validator: validateEaiReg, trigger: "blur" }],
        phone: [{ required: true, validator: validatePhone, trigger: "blur" }],
        age: [{ validator: validateAge, trigger: "blur" }],
      },
    };
  },
  methods: {
    register() {
      this.$refs.registerForm.validate(async (valid) => {
        if (!valid) return false;
        else {
          Object.keys(this.registerForm).map((key) => {
            if (!this.registerForm[key]) delete this.registerForm[key];
          });
          this.registerForm.password = encrypt(this.registerForm.password);
          // this.registerForm.phone = this.SmsForm.phone;
          delete this.registerForm["checkPass"];
          this.axios
            .post("/user/register", this.registerForm)
            .then((res) => {
              console.log(res);
              if (res.data.data.code == 200) {
                this.$confirm("注册成功", "", {
                  confirmButtonText: "前往登录",
                  cancelButtonText: "取消",
                  type: "warning",
                })
                  .then(() => {
                    this.$router.push("/login");
                  })
                  .catch(() => {});
              } else {
                this.$message.error("注册失败");
              }
            })
            .catch(() => {
              this.$message.error("出错了哦");
            });
        }
      });
    },
    login() {
      this.$router.push("/login");
    },
  },
};
</script>


<style scoped>
.re-html {
  width: 100%;
  height: 100%;
  position: fixed;
  background-color: #ffffff;
}
.re-img {
  width: 371px;
  height: 100%;
  float: left;
  background-size: 100% 100%;
  background-image: url("../assets/images/re.jpg");
}
.re-body {
  width: 100%;
  height: 100%;
  /* float: right; */
  position: relative;
  background-size: 100% 100%;
  background-image: url("../assets/images/jjj.jpg");
}
.re-box {
  width: 370px;
  height: 250px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 25px;
  background-color: #d2e2ffa6;
}
.re-header {
  margin-top: 20px;
  height: 20px;
}
.re-header span {
  font-size: 20px;
  display: block;
  text-align: center;
}
.rebox {
  width: 350px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}
.uInfo-input-right {
  position: absolute;
  line-height: 48px;
  height: 100%;
  right: 31px;
  top: 2px;
  z-index: 999;
}

.uInfo-get-code {
  border: none;
  cursor: pointer;
  background-color: transparent;
  display: inline-block;
  height: 20px;
  color: #007dff;
  font-size: 14px;
}
</style>
<style >
.rebox .el-input__inner {
  background-color: #f2f2f2;
  border-radius: 20px;
  border: 1px solid #dcdfe6;
  box-sizing: border-box;
  color: #606466;
  font-size: inherit;
  height: 50px;
  outline: 0;
  padding: 0 15px;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  width: 100%;
}
</style>